import {ref} from "vue";
import {requestAddTemplate, delCarriages} from '/src/API/sendRequest.js'
import {getTableData} from '/src/Views/transit/freight.js'
// 弹框标题
let title = ref('')

// 控制弹出框显示隐藏
const dialogShow = ref(false)
/*  表单类数据   */
// 获取表单实例对象
const ruleFormRef = ref()
// 表单规则
const rules = ({
    templateType: [
        {required: true, message: '请选择对应模板类型', trigger: 'blur'},
    ],
    transportType: [
        {required: true, message: '请选择对应运送类型', trigger: 'blur'},
    ],
    associatedCityList: [
        {required: true, message: '请选择对应关联的城市', trigger: 'blur'},
    ],
    firstWeight: [
        {required: true, message: '请输入首重价格', trigger: 'change'},
        // pattern正则判断
        {pattern: /^\d+$/, message: '请输入符合条件的数字', trigger: 'change'}
    ],
    continuousWeight: [
        {required: true, message: '请输入续重价格', trigger: 'change'},
        // pattern正则判断
        {pattern: /^\d+$/, message: '请输入符合条件的数字', trigger: 'change'}
    ],
    lightThrowingCoefficient: [
        {required: true, message: '请输入轻抛系数', trigger: 'change'},
        // pattern正则判断
        {pattern: /^\d+$/, message: '请输入符合条件的数字', trigger: 'change'}
    ]
})
// 收集表单数据
const formData = ref({
    templateType: '',
    transportType: '',
    associatedCityList: ['1'],
    firstWeight: '',
    continuousWeight: '',
    lightThrowingCoefficient: ''
})

// 弹框关闭提示
const handleClose = () => {
    ElMessageBox.confirm('确定要关闭此窗口吗?', '警告！', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
    })
        .then(() => {
            // 确定之后关闭弹窗
            dialogShow.value = false
            // 将表单清空
            formData.value = {
                templateType: '',
                transportType: '',
                associatedCityList: ['1'],
                firstWeight: '',
                continuousWeight: '',
                lightThrowingCoefficient: ''
            }
            // 清除校验规则
            ruleFormRef.value.resetFields()
        })
        .catch((error) => {
            console.log(error)
        })
}

/*
* 下拉框显示数据
*
* */
// 模板类型下拉框数据
const templateTypeList = ref([
    {
        value: 1,
        label: '同城寄',
    },
    {
        value: 2,
        label: '省内寄',
    },
    {
        value: 3,
        label: '经济区互寄',
    },
    {
        value: 4,
        label: '跨省',
    },
])
// 运送类型下拉框数据
const transportTypeList = ref([
    {
        value: 1,
        label: '普快'
    },
    {
        value: 2,
        label: '特快'
    }
])
// 关联城市下拉框数据
const associatedCityList = ref([
    {
        value: '1',
        label: '全国'
    },
    {
        value: '2',
        label: '京津冀'
    },

    {
        value: '3',
        label: '江浙沪'
    }, {
        value: '4',
        label: '川渝'
    },
    {
        value: '5',
        label: '黑吉辽'
    }
])

/*  业务代码    */

// 新增模板功能
export async function addTemplate(ruleFormRef) {
    await ruleFormRef.validate()
    const res = await requestAddTemplate(formData)
    if (res.msg === 'ok') {
        ElMessage.success(res.msg)
        dialogShow.value = false
        formData.value = {
            templateType: '',
            transportType: '',
            associatedCityList: ['1'],
            firstWeight: '',
            continuousWeight: '',
            lightThrowingCoefficient: ''
        }
        ruleFormRef.resetFields()
        await getTableData()
    } else {
        ElMessage.error(res.msg)
    }
}

// 编辑功能
const handleEdit = (index, row) => {
    title.value = '编辑模板'
    dialogShow.value = true
    formData.value = row
}

// 删除功能
const handleDelete = async (index, row) => {
    ElMessageBox.confirm(
        '确定要删除此条数据吗?',
        '警告！',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error',
        }
    )
        .then(async () => {
            const res = await delCarriages(row.id)
            if (res.msg === 'ok') {
                ElMessage.success('删除成功')
                getTableData()
            } else {
                ElMessage.error(res.msg)
            }
        })
        .catch(() => {

        })
}

/*  导出  */
export {
    title,
    dialogShow,
    ruleFormRef,
    rules,
    formData,
    templateTypeList,
    transportTypeList,
    associatedCityList,
    handleEdit,
    handleDelete,
    handleClose
}
